package org.ddd.orderdemo.remote.service;

import org.ddd.orderdemo.domain.model.product.Product;
import org.ddd.orderdemo.remote.client.ProductApiClient;
import org.ddd.orderdemo.remote.client.dto.ProductDto;
import org.ddd.orderdemo.remote.client.response.BaseRemoteResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;
import java.util.List;

public class ProductRemoteServiceAdapter {

    private Logger logger = LoggerFactory.getLogger(ProductRemoteService.class);

    private ProductApiClient productApiClient;

    private ProductRemoteServiceTranslator productRemoteServiceTranslator;

    public List<Product> getProductInfos(List<Long> productIds) {
        BaseRemoteResponse<List<ProductDto>> response = productApiClient.getProductInfos(productIds);

        if (response == null || response.failed()) {
            logger.error("getProductInfos error,request:{},response:{}", productIds, response);
            return Collections.emptyList();
        }

        List<Product> products = productRemoteServiceTranslator.toProducts(response.getData());

        return products;
    }
}
